import TableList from '@/components/TableList';
import {PageContainer} from '@ant-design/pro-components';
import '@umijs/max';
import dayjs from 'dayjs';
import {message, Modal,Badge, Drawer} from "antd";
import FormModal from "@/components/FormModal";
import {useRef, useState} from "react";
import TOButton from "@/components/TableList/TOButton";
import TableOperations from "@/components/TableList/TableOperations";
import { changeSelectValues } from '@/components/TableList/utils';
import { EnableState, OnlineState } from '@/common/Enums';
import { licenseCreate, licenseDelete, licenseList, licenseUpdate } from './service';
import LicenseFormMgr from './components/LicenseForm';

const LicenseMgr = () => {
  const [formModalVisible, setFormModalVisible] = useState(false);
  const [selectRecord, setSelectRecord] = useState(null);
  const tableRef = useRef();
  const formRef = useRef();
  const {confirm} = Modal;

  const columns = [
    {
      title: '序号',
      align: 'center',
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 50,
    },
    {
      title: 'ID',
      dataIndex: 'id',
      width: 100,
      hideInSearch: true,
      hidden: true
    },
    {
      title: 'license名称',
      dataIndex: 'licenseName',
      width: 100,
    },
    {
      title: 'licenseKey',
      dataIndex: 'licenseKey',
      width: 100,
    },
    {
      title: '在线状态',
      dataIndex: 'onlineState',
      width: 80,
      hideInSearch: true,
      valueEnum: {
        ...changeSelectValues(OnlineState),
      }
    },
    {
      title: '启停状态',
      dataIndex: 'enableState',
      width: 80,
      valueEnum: {
        ...changeSelectValues(EnableState),
      }
    },
    {
      title: '生效时间',
      dataIndex: 'effectiveTime',
      width: 150,
      hideInSearch: true,
    },
    {
      title: '过期时间',
      dataIndex: 'expireTime',
      width: 150,
      hideInSearch: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align: 'center',
      fixed: 'right',
      width: 100,
      search: false,
      render: (_, record) => {
        const operations = [];

        operations.push(<TOButton
          key='edit'
          type='primary'
          text='修改'
          onClick={() => {
            setSelectRecord(record)
            setFormModalVisible(true)
          }}
        />)
        operations.push(<TOButton
          key='del'
          type='danger'
          text='删除'
          onClick={() => {
            confirm({
              title: '删除授权',
              content: `是否删除“${record.licenseName}”授权？`,
              async onOk() {
                await licenseDelete({id: record.id});
                message.success('删除成功')
                tableRef.current.reload();
              }
            });
          }}
        />)
        return <TableOperations operations={operations} /> ;
      }
    }
  ];

  const addCallback = () => {
    if (selectRecord) setSelectRecord(null)
    setFormModalVisible(true)
  };

  return (
    <PageContainer>
      <TableList
        tableRef={tableRef}
        columns={columns}
        keyName="id"
        labelWidth={95}
        tableRequest={licenseList}
        addCallback={addCallback}
        toolBarLeftArea="License列表"
      />
      <FormModal
        title={`${selectRecord ? '修改' : '新增'}License`}
        formRef={formRef}
        // initFormValue={selectRecord || {isCheck: true, state: true}}
        visible={formModalVisible}
        onVisibleChange={visible => {
          setFormModalVisible(visible)
          if (!visible && selectRecord) {
            setSelectRecord(null)
          }
        }}
        labelCol={5}
        onConfirm={
          async (values) => {
            let request = selectRecord ? licenseUpdate : licenseCreate
            if (selectRecord) values.id = selectRecord.id
            values.enableState = values.enableState ? 1 : 0
            let dateTimeFormart = 'YYYY-MM-DD HH:mm:ss';
            values.effectiveTime = dayjs(values.effectiveTime).format(dateTimeFormart);
            values.expireTime = dayjs(values.expireTime).format(dateTimeFormart);
            try {
              await request(values)
              message.success('提交成功')
              tableRef.current.reload()
              return true
            } catch (e) {
              return false
            }
          }
        }
      >
        <LicenseFormMgr
          formRef={formRef}
          record={selectRecord}
        />
      </FormModal>
    </PageContainer>
  );
};
export default LicenseMgr;
